﻿using System;
using System.Collections.Generic;
using System.Linq;

// 学生类
public class Student
{
    public string StudentId { get; set; }     // 学号
    public string Name { get; set; }          // 姓名
    public int Age { get; set; }              // 年龄

    public Student(string studentId, string name, int age)
    {
        StudentId = studentId;
        Name = name;
        Age = age;
    }

    public override string ToString()
    {
        return $"学号：{StudentId}，姓名：{Name}，年龄：{Age}";
    }
}

// 学生管理类
public class StudentManager
{
    private List<Student> students = new List<Student>();

    // 1. 增加一个学生
    public void AddStudent(Student student)
    {
        students.Add(student);
        Console.WriteLine($"成功添加学生：{student}");
    }

    // 2. 修改一个指定学号的学生
    public void UpdateStudent(string studentId, string newName, int newAge)
    {
        Student student = FindStudent(studentId);
        if (student != null)
        {
            student.Name = newName;
            student.Age = newAge;
            Console.WriteLine($"成功修改学生信息：{student}");
        }
        else
        {
            Console.WriteLine($"错误：找不到学号为 {studentId} 的学生");
        }
    }

    // 3. 删除一个学生
    public void DeleteStudent(string studentId)
    {
        Student student = FindStudent(studentId);
        if (student != null)
        {
            students.Remove(student);
            Console.WriteLine($"成功删除学号为 {studentId} 的学生");
        }
        else
        {
            Console.WriteLine($"错误：找不到学号为 {studentId} 的学生");
        }
    }

    // 4. 查询一个指定学号的学生
    public Student FindStudent(string studentId)
    {
        return students.FirstOrDefault(s => s.StudentId == studentId);
    }

    // 5. 遍历所有学生
    public void DisplayAllStudents()
    {
        if (students.Count == 0)
        {
            Console.WriteLine("当前没有学生信息");
            return;
        }

        Console.WriteLine("\n=== 所有学生信息 ===");
        foreach (var student in students)
        {
            Console.WriteLine(student);
        }
    }
}

// 测试类
class Program
{
    static void Main(string[] args)
    {
        StudentManager manager = new StudentManager();

        Console.WriteLine("=== 学生管理系统测试 ===");

        // 测试增加学生
        Console.WriteLine("\n1. 测试增加学生功能");
        manager.AddStudent(new Student("001", "张三", 20));
        manager.AddStudent(new Student("002", "李四", 21));
        manager.AddStudent(new Student("003", "王五", 19));

        // 测试遍历所有学生
        Console.WriteLine("\n2. 测试遍历所有学生");
        manager.DisplayAllStudents();

        // 测试查询学生
        Console.WriteLine("\n3. 测试查询学生");
        Student foundStudent = manager.FindStudent("002");
        if (foundStudent != null)
        {
            Console.WriteLine($"找到学生：{foundStudent}");
        }

        // 测试修改学生
        Console.WriteLine("\n4. 测试修改学生");
        manager.UpdateStudent("002", "李四四", 22);

        // 再次遍历查看修改结果
        manager.DisplayAllStudents();

        // 测试删除学生
        Console.WriteLine("\n5. 测试删除学生");
        manager.DeleteStudent("001");

        // 最终的学生列表
        Console.WriteLine("\n6. 最终学生列表");
        manager.DisplayAllStudents();

        Console.WriteLine("\n=== 测试完成 ===");
        Console.ReadKey();
    }
}